/**
 * @description 根据val(数字或字符串) 生成一个字符串并返回,
 * @param val
 * @param digitNum 规定保留多少位小数
 * @returns {string}
 */
import { USER_ORIENTED } from '../config';
import { CHAT_HOUSE_CARD } from '../misc';
import { MSG_TYPE } from '../rongCloud/messaging';
import { _getTimeStringAutoShort2 } from './chatMixin';

function subStringNum(val, digitNum) {
  var a_type = typeof val;
  if (a_type === 'number') {
    var aStr = val.toString();
    var aArr = aStr.split('.');
  } else if (a_type === 'string') {
    aArr = val.split('.');
  }

  if (aArr.length > 1) {
    val = aArr[0] + '.' + aArr[1].substr(0, digitNum);
  }
  return val
}

export const filter = {
  filters: {
    // 首字母大写过滤器
    capitalize: function (value) {
      if (!value) return '';
      value = value.toString();
      return value.charAt(0).toUpperCase() + value.slice(1);
    },
    tenMillion_cn(val) {
      if (val > 10000) {
        let num = (val / 10000).toFixed(2);
        return num + '万';
      } else return val;
    },

    tenMillion(val, digitNum = 2) {
      if (val > 10000) {
        val = val / 10000;
      }
      let forFixed = Math.pow(10, digitNum);
      val = Math.round(val * forFixed) / forFixed;
      return subStringNum(val, digitNum);
    },
    removeDigit(val) {
      if (val) {
        let t = val.split('.');
        return t[0];
      } else {
        return '';
      }
    },
    chatTime(val) {
      val = val.slice(5);
      val = val.substring(0, val.length - 3);
      return val;
    },
  },
  methods: {
    yesNo(val) {
      if (val === 1) return '是';
      return '否';
    },
    rongChatTime(val) {
      function myChatTime() {
        var date = new Date(val);
        let M =
          date.getMonth() + 1 < 10
            ? '0' + (date.getMonth() + 1)
            : date.getMonth() + 1;
        let D = date.getDate();
        let h = makeIt_2Digit(date.getHours().toString());
        let m = makeIt_2Digit(date.getMinutes().toString());
        function makeIt_2Digit(val) {
          if (val.length === 1) {
            return '0' + val;
          }
          return val;
        }

        return M + '-' + D + ' ' + h + ':' + m;
      }
      // return myChatTime();
      return _getTimeStringAutoShort2(val, true);
    },
    getTimeFromStamp(val) {
      var date = new Date(val);
      let M =
        date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1;
      let D = date.getDate();
      let h = makeIt_2Digit(date.getHours().toString());
      let m = makeIt_2Digit(date.getMinutes().toString());

      function makeIt_2Digit(val) {
        if (val.length === 1) {
          return '0' + val;
        }
        return val;
      }

      return M + '-' + D + ' ' + h + ':' + m;
    },
    profilePhoto(obj) {
      if (!obj) return;
      if (obj.headimgurl) {
        return obj.headimgurl;
      } else if (obj.head_thumb) {
        return obj.head_thumb;
      } else if (obj.portrait) {
        return obj.portrait;
      }
    },
    displayChatName(person) {
      if (!person) {
        return USER_ORIENTED ? '招商人员' : '客户';
      }
      // console.log(person)
      //
      if (!USER_ORIENTED) {
        // 招商端
        return person.userNamed || person.name;
      } else {
        // 用户端
        return person.name;
      }
    },
    displayChatContent(message) {
      if (message.messageType === CHAT_HOUSE_CARD) {
        return '[房源卡片]';
      } else if (message.messageType === MSG_TYPE().TEXT) {
        return message.content.content;
      } else if (message.messageType === MSG_TYPE().AUDIO) {
        return '[语音]';
      } else if (message.messageType === MSG_TYPE().IMAGE) {
        return '[图片]';
      } else {
        return '[新消息]';
      }
    },
  },
};

// 过滤键值对
// export function filteredKeys(obj,  )
